//
//  NetworkingViewController.m
//  Honda
//
//  Created by Wang.Xinlei on 4/22/13.
//  Copyright (c) 2013 HeninHoo. All rights reserved.
//

#import "NetworkingViewController.h"

@interface NetworkingViewController ()

@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
@property (retain, nonatomic) IBOutlet UILabel *titleLabel;
@property (retain, nonatomic) IBOutlet UILabel *contentLabel;

@end

@implementation NetworkingViewController

@dynamic title, content, activiting;

+ (NetworkingViewController *)sharedController {
    static NetworkingViewController * s_sharedController = nil;
    @synchronized(s_sharedController) {
        if ([NetworkingViewController invalid:s_sharedController]) {
            s_sharedController = [[NetworkingViewController alloc] initWithNibName:nil bundle:nil];
        }
        
        return s_sharedController;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self view];
        
        self.indicator.hidesWhenStopped = YES;
    }
    return self;
}

- (void)dealloc {
    SafeRelease(_indicator);
    SafeRelease(_titleLabel);
    SafeRelease(_contentLabel);
    
    [super dealloc];
}

- (NSString *)title {
    return self.titleLabel.text;
}

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
    
    [self layoutNetworkingView];
}

- (NSString *)content {
    return self.contentLabel.text;
}

- (void)setContent:(NSString *)content {
    self.contentLabel.text = content;
    
    [self layoutNetworkingView];
}

- (BOOL)activiting {
    return self.indicator.isAnimating;
}

- (void)setActiviting:(BOOL)activiting {
    if (activiting && ! self.activiting) {
        [self.indicator startAnimating];
    }
    else if (! activiting && self.activiting) {
        [self.indicator stopAnimating];
    }
    
    [self layoutNetworkingView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 5;
    
    self.titleLabel.numberOfLines = 0;
    self.contentLabel.numberOfLines = 0;
}

- (void)viewDidUnload {
    [self setContentLabel:nil];
    [self setTitleLabel:nil];
    [self setIndicator:nil];
    
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showNetworkingWithTitle:(NSString *)title content:(NSString *)content activiting:(BOOL)activiting enableDismiss:(BOOL)enableDismiss {
    self.titleLabel.text = title;
    self.contentLabel.text = content;
    
    self.activiting = activiting;
    
    [self showViewController:self enableDismiss:enableDismiss completion:nil];
}

- (void)dismissNetworking {
    [self dismissFromParentViewControllerWithCompletion:nil];
}

#pragma mark 
- (void)layoutNetworkingView {
    self.titleLabel.hidden = [NSString invalidString:self.title];
    self.contentLabel.hidden = [NSString invalidString:self.content];
    self.indicator.hidden = ! self.activiting;
    
    CGRect bounds = self.activiting ? CGRectMake(0, 0, 215, 110) : CGRectMake(0, 0, 260, 110);
    self.titleLabel.frame = (self.titleLabel.hidden
                             ? CGRectZero
                             : [self.titleLabel textRectForBounds:bounds limitedToNumberOfLines:self.titleLabel.numberOfLines]);
    self.contentLabel.frame = (self.contentLabel.hidden
                               ? CGRectZero
                               : [self.contentLabel textRectForBounds:bounds limitedToNumberOfLines:self.contentLabel.numberOfLines]);
    CGFloat widthOfText = fmax(CGRectGetWidth(self.titleLabel.frame), CGRectGetWidth(self.contentLabel.frame));
    if (0 < widthOfText) {
        widthOfText += 8;
    }
    CGFloat widthOfIndicator = self.activiting ? 8 + CGRectGetWidth(self.indicator.bounds) : 0;
    if (self.activiting) {
        self.indicator.center = CGPointMake((300 - widthOfText) / 2, 75);
    }
    
    if (! self.titleLabel.hidden && ! self.contentLabel.hidden) {
        self.titleLabel.center = CGPointMake((300 - widthOfIndicator) / 2, (150 - CGRectGetHeight(self.titleLabel.frame)) / 2);
        self.contentLabel.center = CGPointMake((300 - widthOfIndicator) / 2, (150 + CGRectGetHeight(self.contentLabel.frame)) / 2);
    }
    else if (self.titleLabel.hidden) {
        self.contentLabel.center = CGPointMake((300 + widthOfIndicator) / 2, 75);
    }
    else if (self.contentLabel.hidden) {
        self.titleLabel.center = CGPointMake((300 + widthOfIndicator) / 2, 75);
    }
}

@end
